﻿Imports System.Collections.Generic
Imports lablinkhelper
Imports System.IO.Ports

Public Class FrmPortManagement

#Region "Attributes"

    ReadOnly _portList As List(Of String) = ComPortUtilities.GetAllSerialPort()
    Private Const CongDaDuocMo As String = "Cổng đã được Mở"
    Private Const CongChuaDuocMo As String = "Cổng chưa được Mở"

    Dim _currentPort As SerialPort

#End Region

    ''' <summary>
    ''' Hàm thực thi khi load form
    ''' </summary>
    ''' <param name="sender">The sender.</param>
    ''' <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
    Private Sub FrmPortManagementLoad(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

        'Sắp xếp lại tên port
        _portList.Sort()

        LoadAllPort()

        For Each port As SerialPort In gv_PortInUse
            
            Dim tempbtn As Windows.Forms.Button
            tempbtn = New Button
            tempbtn.Tag = port
            tempbtn.AutoSize = True
            tempbtn.Location = New Point(3, 0)
            tempbtn.Name = port.PortName
            tempbtn.Size = New Size(lpnInUse.Width - 20, 20)
            tempbtn.TabIndex = 0
            tempbtn.Text = port.PortName

            lpnInUse.Controls.Add(tempbtn)
            AddHandler tempbtn.Click, AddressOf ButtonClick
        Next

        If lpnInUse.Controls.Count > 0 Then
            Dim tempbtn As Windows.Forms.Button = lpnInUse.Controls.Item(0)
            tempbtn.PerformClick()
        End If


    End Sub

#Region "Private Method"

    ''' <summary>
    ''' Load tất cả các port có trong hệ thống
    ''' </summary>
    Private Sub LoadAllPort()
        For Each port As String In _portList
            Dim templbl As New Button
            templbl.AutoSize = True
            templbl.Location = New Point(3, 0)
            templbl.Name = port.Replace(" ", "")
            templbl.Size = New Size(100, 20)
            templbl.TabIndex = 0
            templbl.Text = port
            LpnAllPort.Controls.Add(templbl)
        Next
    End Sub


    ''' <summary>
    ''' Hàm xử lý sự kiện bấm nút
    ''' </summary>
    ''' <param name="sender">The sender.</param>
    ''' <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
    Private Sub ButtonClick(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim obj As Button = sender
        _currentPort = obj.Tag
        txtBaudRate.Text = _currentPort.BaudRate.ToString()
        txtPortName.Text = _currentPort.PortName
        txtDataBit.Text = _currentPort.DataBits.ToString()
        txtStopBit.Text = _currentPort.StopBits.ToString()
        txtDTR.Text = _currentPort.DtrEnable.ToString()
        txtRTS.Text = _currentPort.RtsEnable.ToString()
        txtStatus.Text = CheckPortStatus(_currentPort)

        If gv_PortDictionary.ContainsKey(_currentPort.PortName) Then
            txtDevice.Text = gv_PortDictionary.Item(_currentPort.PortName)
            
        End If
        If txtStatus.Text = CongChuaDuocMo Then
            btnOpenClose.Tag = 0
            btnOpenClose.Text = "Mở Cổng"
            pbxConnected.Image = ImageList1.Images(0)

        Else
            btnOpenClose.Tag = 1
            btnOpenClose.Text = "Đóng Cổng"
            pbxConnected.Image = ImageList1.Images(1)
        End If
    End Sub

    Private Function CheckPortStatus(ByVal mySerialPort As SerialPort) As String
        Dim result As String
        Try
            mySerialPort.Open()
            mySerialPort.Close()
            result = CongChuaDuocMo
        Catch ex As Exception
            result = CongDaDuocMo
        End Try
        Return result
    End Function

#End Region


    Private Sub btnOpenClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenClose.Click
        If btnOpenClose.Tag = 0 Then
            Try
                _currentPort.Open()
                btnOpenClose.Tag = 1
                btnOpenClose.Text = "Đóng Cổng"
                txtStatus.Text = CongDaDuocMo
                pbxConnected.Image = ImageList1.Images(1)
                If txtDevice.Text = "ADVIA2120" Then
                    If Not String.IsNullOrEmpty(InitAdvia2120String) Then
                        _currentPort.Write(InitAdvia2120String)
                        blnInitAdvia2120 = True
                        If gv_PortCount.ContainsKey(_currentPort.PortName) Then
                            gv_Result(2, gv_PortCount.Item(_currentPort.PortName)) = [String].Empty
                        End If
                        MT = &H30
                    End If
                    
                End If

                
            Catch ex As Exception
                btnOpenClose.Tag = 0
                btnOpenClose.Text = "Mở Cổng"
                txtStatus.Text = CongChuaDuocMo
                pbxConnected.Image = ImageList1.Images(0)
            End Try

        Else
            Try
                _currentPort.Close()
                btnOpenClose.Tag = 0
                btnOpenClose.Text = "Mở Cổng"
                txtStatus.Text = CongChuaDuocMo
                pbxConnected.Image = ImageList1.Images(0)
            Catch ex As Exception
                btnOpenClose.Tag = 1
                btnOpenClose.Text = "Đóng Cổng"
                txtStatus.Text = CongDaDuocMo
                pbxConnected.Image = ImageList1.Images(1)
            End Try
        End If
    End Sub
End Class